package com.gec.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@Controller
@RequestMapping("/captcha")
public class CaptchaController {

    //定义一些值用于生产验证码的图片

    //宽度
    private static final int WIDTH = 120;

    //高度
    private static final int HEIGHT = 30;

    //起始位置
    private static final int DRAWY = 18;

    //文字的间隔
    private static int SPACE = 18;

    //验证码长度
    private static final int CHARCOUNT = 6;

    //验证码数组内容
    private static final String[] CHARS = {"a", "b", "c", "d", "e", "f", "g",
            "h", "i", "j", "k", "l", "m", "n",
            "o", "p", "q", "r", "s", "t",
            "u", "v", "w", "x", "y", "z"
            , "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

    //定义方法,用于生产验证码内容
    @RequestMapping("/code")
    public void makeCaptchaCode(HttpServletResponse response, HttpServletRequest request) throws IOException {
        /**
         * 验证码:需要在内存中绘制一个图片BufferedImage
         * 向这个图片中写入文字,并把绘制好内容的图片响应给请求
         */
        //创建一个背景透明的图片,使用rgb表示颜色
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

        //获取画笔
        Graphics graphics = image.getGraphics();

        //设置背景色
        graphics.setColor(Color.white);

        //给image画板涂成白色的,画成一个矩形
        graphics.fillRect(0, 0, WIDTH, HEIGHT);

        //把画笔的颜色设置成黑色,方便写字
        graphics.setColor(Color.BLACK);

        //画内容
        //设置画布上的字体格式
        Font font = new Font("宋体", Font.BOLD, 16);
        graphics.setFont(font);

        //生成StringBuffer存储验证码信息
        StringBuilder buffer = new StringBuilder();

        //在画布上写字
        int ran = 0;
        int len = CHARS.length;
        for (int i = 0; i < CHARCOUNT; i++) {
            ran = new Random().nextInt(len);
            graphics.setColor(makeColor());
            graphics.drawString(CHARS[ran], (i + 1) * SPACE, DRAWY);
            buffer.append(CHARS[ran]);
        }

        //将buffer存入session中
        HttpSession session = request.getSession();
        session.setAttribute("code", buffer.toString());

        //绘制干扰线
        for (int i = 0; i < 4; i++) {
            graphics.setColor(makeColor());

            int[] dot = makeLineDot();

            graphics.drawLine(dot[0], dot[1], dot[2], dot[3]);
        }

        //设置没有缓存
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        //设置响应图片的格式
        response.setContentType("image/png");

        ServletOutputStream out = response.getOutputStream();

        //写出图片
        ImageIO.write(image, "png", out);

        out.flush();
        out.close();
    }

    //验证码随机颜色
    private Color makeColor() {
        Random random = new Random();
        int r = random.nextInt(255);
        int g = random.nextInt(255);
        int b = random.nextInt(255);

        return new Color(r, g, b);
    }

    //随机端点生成干扰线
    private int[] makeLineDot() {
        Random random = new Random();

        int x1 = random.nextInt(WIDTH / 2);
        int y1 = random.nextInt(HEIGHT);

        int x2 = random.nextInt(WIDTH);
        int y2 = random.nextInt(HEIGHT);
        return new int[]{x1, y1, x2, y2};
    }
}
